﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BLL;
using UserUtility;

namespace TrainingWorkshop
{
    public partial class CreateNewUser : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            Bind();
        }

        private void Bind()
        {
            IUserRepository userRepository = UserRepositoryFactory.InitUserRepository();

            //DataRow departmentrow = Createddropdownlist.NewRow();
            //departmentrow["DepartmentName"] = "(Please Select)";
            //departmentrow["DepartmentId"] = "0";
            //Createddropdownlist.Rows.InsertAt(departmentrow, 0);

            CreateDepartmentDropDownList.DataSource = userRepository.GetAllDepartment();

            CreateDepartmentDropDownList.Items.Insert(0, new ListItem("Please Select", "0"));
           
            CreateDepartmentDropDownList.DataTextField = "DepartmentName";
            CreateDepartmentDropDownList.DataValueField = "DepartmentId";
            CreateDepartmentDropDownList.DataBind();

          

            //DataRow rolerow = CreateRdropdownlist.NewRow();
            //rolerow["RoleName"] = "(Please Select)";
            //rolerow["RoleId"] = "0";
            //CreateRdropdownlist.Rows.InsertAt(rolerow, 0);

            CreateRoleDropDownList.Items.Insert(0, new ListItem("Please Select", "0"));

            CreateRoleDropDownList.DataSource = userRepository.GetAllRole();

            CreateRoleDropDownList.DataTextField = "RoleName";
            CreateRoleDropDownList.DataValueField = "RoleId";
            CreateRoleDropDownList.DataBind();
        }
        protected void CreateAddButton_Click(object sender, EventArgs e)
        {
            User User = new User();
            User.FirstName = CreateFirstNameTextBox.Text;
            User.LastName = CreateLastNameTextBox.Text;
            User.departmentId.DepartmentId = int.Parse(CreateDepartmentDropDownList.SelectedValue);
            User.roleId.RoleId = int.Parse(CreateRoleDropDownList.SelectedValue);
            IUserRepository userRepositoryCreate = UserRepositoryFactory.InitUserRepository();
            userRepositoryCreate.Add(User);
            //if (createnew)
            
                Response.Write("<script>alert('Add successfully');window.location.href='UserList.aspx'</script>");
                Bind();
            //}
            //else
            //{
            //    Response.Write("<script>alert('Failed ')</script>");
            //}
        }
    }
}